import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/Login'
// import Home from '@/views/Home'
// import Layout from '@/views/Layout'
// import User from '@/views/User'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult.vue'
// import ArticleDetail from '@/views/ArticleDetail'
// import UserEdit from '@/views/User/UserEdit'
// import Chat from '@/views/Chat'
import { getToken } from '@/utils/token.js'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/layout/home'
}, {
  path: '/login',
  component: () => import(/* webpackChunkName: "Login" */ '@/views/Login')
}, {
  path: '/layout',
  component: () => import(/* webpackChunkName: "Layout" */ '@/views/Layout'),
  redirect: '/layout/home',
  children: [{
    path: 'home',
    component: () => import(/* webpackChunkName: "Home" */ '@/views/Home'),
    meta: {
      scrollT: 0// 保存离开首页时滚动条的位置
    }
  }, {
    path: 'user',
    component: () => import(/* webpackChunkName: "User" */ '@/views/User')
  }]
}, {
  path: '/search',
  component: () => import(/* webpackChunkName: "Search" */ '@/views/Search')
}, {
  path: '/search_result/:kw',
  component: () => import(/* webpackChunkName: "SearchResult" */ '@/views/Search/SearchResult.vue')
}, {
  path: '/detail',
  component: () => import(/* webpackChunkName: "ArticleDetail" */ '@/views/ArticleDetail')
}, {
  path: '/user_edit',
  component: () => import(/* webpackChunkName: "UserEdit" */ '@/views/User/UserEdit')
}, {
  path: '/chat',
  component: () => import(/* webpackChunkName: "Chat" */ '@/views/Chat')
}]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (getToken()?.length > 0 && to.path === '/login') {
    next('/layout/home')
  } else {
    next()
  }
})

export default router
